package com.luban.bio;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @ClassName SocketServer
 * @Description 498982703@qq.com
 * @Author gary
 * @Date 2021/2/19 8:55
 * @Version 1.0
 */
public class SocketServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9000);
        while (true) {
            System.out.println("等待连接。。");
            Socket accept = serverSocket.accept();
            System.out.println("have create link");
            handler(accept);
        }

    }

    private static void handler(Socket client)throws IOException{
        InputStream inputStream = client.getInputStream();
        byte[] bytes = new byte[1024];
        int read = inputStream.read(bytes);
        System.out.println("read over");
        if (read !=-1) {
            System.out.println("accept client data:" + new String(bytes, 0, read));
        }
        client.getOutputStream().write("hello.client".getBytes());
        client.getOutputStream().flush();

    }
}
